package com.ymttest.common.entitie;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ymttest.common.enums.DateTypeEnum;

/**
 * 
 * @author songyefeng
 * @field startTime
 * @field endTime
 */
public class TimeRange {
	private String startTime;
	private String endTime;
	private static final String YMD = "yyyy-MM-dd";
	private static TimeRange range = new TimeRange();

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	/**
	 * 返回yyyy-MM-dd是日期字符串
	 * 
	 * @param calendar
	 * @return
	 */
	public static String calendar2YMDString(Calendar calendar) {
		return new SimpleDateFormat(YMD).format(calendar.getTime());
	}

	/**
	 * 获取今年1月1号的calendar
	 * 
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Calendar getFirstDateOfThisYear() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1 * calendar.getTime().getMonth());
		calendar.set(Calendar.DATE, 1);
		return calendar;
	}

	/**
	 * 获取明年1月1号的calendar
	 * 
	 * @return
	 */
	public static Calendar getFirstDateOfNextYear() {
		Calendar calendar = getFirstDateOfThisYear();
		calendar.add(Calendar.YEAR, 1);
		return calendar;
	}

	/**
	 * 获取2个月前1号的calendar
	 * 
	 * @return
	 */
	public static Calendar getFirstDateOfTwoMonthAgo() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -2);
		calendar.set(Calendar.DATE, 1);
		return calendar;
	}

	/**
	 * 获取下月1号的calendar
	 * 
	 * @return
	 */
	public static Calendar getFirstDateOfNextMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DATE, 1);
		return calendar;
	}

	/**
	 * 获取这个月1号的calendar
	 * 
	 * @return
	 */
	public static Calendar getFirstDateOfThisMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, 1);
		return calendar;
	}

	/**
	 * 获取这周一的Calendar
	 * 
	 * @return
	 */
	public static Calendar getFirstDayOfThisWeek() {
		Calendar calendar = Calendar.getInstance();
		@SuppressWarnings("deprecation")
		int day = calendar.getTime().getDay() == 0 ? 7 : calendar.getTime()
				.getDay();
		calendar.add(Calendar.DAY_OF_WEEK, 1 - day);
		return calendar;
	}

	/**
	 * 获取下周一的Calendar
	 * 
	 * @return
	 */
	public static Calendar getFirstDayOfNextWeek() {
		Calendar calendar = getFirstDayOfThisWeek();
		calendar.add(Calendar.DAY_OF_WEEK, 7);
		return calendar;
	}

	/**
	 * 获取明天的Calendar
	 * 
	 * @return
	 */
	public static Calendar getNextDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, 1);
		return calendar;
	}

	/**
	 * 获取昨天的Calendar
	 * 
	 * @return
	 */
	public static Calendar getYesterDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		return calendar;
	}

	public static TimeRange getRangeOfThisYear() {
		String startTime = calendar2YMDString(getFirstDateOfThisYear());
		String endTime = calendar2YMDString(getFirstDateOfNextYear());
		range.setStartTime(startTime);
		range.setEndTime(endTime);
		return range;
	}

	public static TimeRange getRangeOfThisQuarter() {
		String startTime = calendar2YMDString(getFirstDateOfTwoMonthAgo());
		String endTime = calendar2YMDString(getFirstDateOfNextMonth());
		range.setStartTime(startTime);
		range.setEndTime(endTime);
		return range;
	}

	public static TimeRange getRangeOfThisMonth() {
		String startTime = calendar2YMDString(getFirstDateOfThisMonth());
		String endTime = calendar2YMDString(getFirstDateOfNextMonth());
		range.setStartTime(startTime);
		range.setEndTime(endTime);
		return range;
	}

	public static TimeRange getRangeOfToday() {
		String startTime = calendar2YMDString(Calendar.getInstance());
		String endTime = calendar2YMDString(getNextDate());
		range.setStartTime(startTime);
		range.setEndTime(endTime);
		return range;
	}

	public static TimeRange getRangeOfYesterDay() {
		String startTime = calendar2YMDString(getYesterDay());
		String endTime = calendar2YMDString(Calendar.getInstance());
		range.setStartTime(startTime);
		range.setEndTime(endTime);
		return range;
	}

	public static TimeRange getRangeOfThisWeek() {
		String startTime = calendar2YMDString(getFirstDayOfThisWeek());
		String endTime = calendar2YMDString(getFirstDayOfNextWeek());
		range.setStartTime(startTime);
		range.setEndTime(endTime);
		return range;
	}

	/**
	 * 根据DateTypeEnum的枚举值列表，生成时间范围，格式yyyy-MM-dd
	 * 
	 * @param dateList
	 *            com.ymttest.common.enums.DateTypeEnum的枚举值列表
	 * @return
	 */
	public static TimeRange getTimeRangeByDateTypeEnum(List<Integer> dateList) {
		TimeRange tr = null;
		if (dateList.contains(DateTypeEnum.All.getValue())
				|| dateList.contains(DateTypeEnum.ThisYear.getValue())
				|| dateList.contains(DateTypeEnum.ThisQuarter.getValue())
				|| dateList.contains(DateTypeEnum.ThisMonth.getValue())
				|| dateList.contains(DateTypeEnum.ThisWeek.getValue())
				|| dateList.contains(DateTypeEnum.Today.getValue())
				|| dateList.contains(DateTypeEnum.Yesterday.getValue())) {
			tr = new TimeRange();
		}
		if (dateList.contains(DateTypeEnum.All.getValue()) == false) {
			if (dateList.contains(DateTypeEnum.ThisYear.getValue())) { // 如果list里面有ThisYear的枚举值
				tr.setStartTime(getRangeOfThisYear().getStartTime()); // timeFrom为今年的1月1号
				tr.setEndTime(getRangeOfThisYear().getEndTime());// timeTo为明年的1月1号
			} else if (dateList.contains(DateTypeEnum.ThisQuarter.getValue())) {// 否则，如果list里面有ThisQuarter的枚举值
				tr.setStartTime(getRangeOfThisQuarter().getStartTime());// timeFrom为这个月的上上月的1号
				tr.setEndTime(getRangeOfThisQuarter().getEndTime());// timeTo为这个月的下月1号
			} else if (dateList.contains(DateTypeEnum.ThisMonth.getValue())) {// 否则，如果list里面有ThisMonth的枚举值
				tr.setStartTime(getRangeOfThisMonth().getStartTime());// timeFrom为这个月的1号
				tr.setEndTime(getRangeOfThisMonth().getEndTime());// timeTo为下个月的1号
			} else if (dateList.contains(DateTypeEnum.Today.getValue())) {// 否则，如果list里面有Today的枚举值
				tr.setStartTime(getRangeOfToday().getStartTime());// timeFrom为今天
				tr.setEndTime(getRangeOfToday().getEndTime());// timeTo为明天
			}
			if (dateList.contains(DateTypeEnum.ThisWeek.getValue())) {// 如果list里面有ThisWeek的枚举值(周可以跨月，跨年)
				String timeFrom = tr.getStartTime();
				String timeTo = tr.getEndTime();
				if (null != timeFrom) {
					SimpleDateFormat sdf = new SimpleDateFormat(YMD);
					Date timeFromDate = null;
					Date timeToDate = null;
					try {
						timeFromDate = sdf.parse(timeFrom);
						timeToDate = sdf.parse(timeTo);
					} catch (ParseException e) {
						e.printStackTrace();
					}
					if (TimeRange.getFirstDayOfThisWeek().getTime()
							.compareTo(timeFromDate) == -1) {// 如果map里面已经存在timeFrom的值，且timeFrom的值大(晚)于这周一，则更新timeFrom的值为这周一
						tr.setStartTime(getRangeOfThisWeek().getStartTime());
					}
					if (TimeRange.getFirstDayOfNextWeek().getTime()
							.compareTo(timeToDate) == 1) {// 如果map里面已经存在timeFrom的值，且timeTo的值小(早)于下周一，则更新timeFrom的值为下周一
						tr.setEndTime(getRangeOfThisWeek().getEndTime());
					}
				} else {
					tr.setStartTime(getRangeOfThisWeek().getStartTime());
					tr.setEndTime(getRangeOfThisWeek().getEndTime());
				}
			}
			if (dateList.contains(DateTypeEnum.Yesterday.getValue())) {// 如果list里面有Yesterday的枚举值
				String timeFrom = tr.getStartTime();
				if (null != timeFrom) {
					SimpleDateFormat sdf = new SimpleDateFormat(YMD);
					Date timeFromDate = null;
					try {
						timeFromDate = sdf.parse(timeFrom);
					} catch (ParseException e) {
						e.printStackTrace();
					}
					if (TimeRange.getYesterDay().getTime()
							.compareTo(timeFromDate) == -1) {// 如果map里面已经存在timeFrom的值，切timeFrom的值大(晚)于昨天，则更新timeFrom的值为昨天
						tr.setStartTime(getRangeOfYesterDay().getStartTime());
					}
				} else {// 如果map里面没有timeFrom
					tr.setStartTime(getRangeOfYesterDay().getStartTime()); // timeFrom的值为昨天
					tr.setEndTime(getRangeOfYesterDay().getEndTime());// timeTo的值为今天
				}
			}
		}
		return tr;
	}

	public static TimeRange getTimeRangeByDateTypeEnum(Integer date) {
		List<Integer> dateList = new ArrayList<Integer>();
		dateList.add(date);
		return getTimeRangeByDateTypeEnum(dateList);
	}

}
